#include <commands\commandFactory.h>
#include <network\streamingConnection.h>

commandFactory *commandFactory::_instance = NULL;

commandFactory &
commandFactory::instance() {
  if (!_instance) {
    _instance = new commandFactory();
  }

  return *_instance;
};

commandFactory::commandFactory() {

}

bool
commandFactory::registerCommand(unsigned int commandId, createCommandCallback createFn) {

  if (commands.find(commandId) == commands.end()) {
    commands.insert(std::pair<unsigned int, createCommandCallback>(commandId, createFn));
  }
  return true;
}

bool
commandFactory::unregisterCommand(unsigned int commandId) { 
  icommands = commands.find(commandId);
  if (icommands != commands.end()) {
    commands.erase(icommands);
    return true;
  }

  return false;
}

command* 
commandFactory::createCommand(unsigned int commandId, streamingConnection *connection) {

  icommands = commands.find(commandId);
  if (icommands != commands.end()) {
    return icommands->second(commandId, connection);
  }

  return NULL;
}